

import junit.framework.TestCase;

public class SegmentTest extends TestCase {

	Segment s1;
	Segment s2;
	Segment s3;

	public void testGetB() {
		s1 = new Segment(new Point(0,0), new Point(1,1));
		s2 = new Segment(new Point(0,0), new Point(0,1));
		s3 = new Segment(new Point(0,0), new Point(0,0));
		try{
			assertTrue(s1.getB()==0);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Exception found");}
		
		try{
			s2.getB();
			fail("No exception");
		} catch (Exception e) {/*ok*/}
		
		try{
			s3.getB();
			fail("No exception");
		} catch (Exception e) {/*ok*/}
	}

	public void testGetC() {
		s1 = new Segment(new Point(0,0), new Point(1,1));
		s2 = new Segment(new Point(0,0), new Point(0,1));
		s3 = new Segment(new Point(0,0), new Point(0,0));
		try{
			s1.getC();
			fail("No exception");
		} catch (Exception e) {/*ok*/}
		
		try{
			assertTrue(s2.getC()==0);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Exception found");}
		
		/*try{
			assertTrue(s3.getC()==Double.POSITIVE_INFINITY);
		} catch (Exception e) {
			e.printStackTrace();
			fail("Exception found");}*/
	}
	
	
	public void testPointIntersection() {
		//Test case1: S1 is diagonal, S2 vertical, no intersection
		s1 = new Segment(new Point(1,1), new Point(2,2));
		s2 = new Segment(new Point(0,0), new Point(0,1));
		try{
			assertTrue(s1.pointIntersection(s2)==null);
			assertTrue(s2.pointIntersection(s1)==null);
			s1.segmentIntersection(s2);
			s2.segmentIntersection(s1);
			fail("No exception found");
		}catch(IntersectsAsSegmentException e){
			e.printStackTrace();
			fail("IntersectsAsSegmentException found");
		} catch (IntersectsAsPointException e) {/*ok*/}
		
		//Test case2: S1 diagonal, S2 vertical, intersection at 1,1
		s1 = new Segment(new Point(0,0), new Point(2,2));
		s2 = new Segment(new Point(1,-10), new Point(1,10));
		try{
			assertTrue(s1.pointIntersection(s2).getX()==1);
			assertTrue(s1.pointIntersection(s2).getY()==1);
			assertTrue(s2.pointIntersection(s1).getX()==1);
			assertTrue(s2.pointIntersection(s1).getY()==1);
			s1.segmentIntersection(s2);
			s2.segmentIntersection(s1);
			fail("No exception found");
		}catch(IntersectsAsSegmentException e){
			e.printStackTrace();
			fail("IntersectsAsSegmentException found");
		} catch (IntersectsAsPointException e) {/*ok*/}
		
		//Test case3: S1 horizontal, S2 diagonal(downwards), intersection at 0,0
		s1 = new Segment(new Point(-5,0), new Point(5,0));
		s2 = new Segment(new Point(-2,2), new Point(2,-2));
		try{
			assertTrue(s1.pointIntersection(s2).getX()==0);
			assertTrue(s1.pointIntersection(s2).getY()==0);
			assertTrue(s2.pointIntersection(s1).getX()==0);
			assertTrue(s2.pointIntersection(s1).getY()==0);
			s1.segmentIntersection(s2);
			s2.segmentIntersection(s1);
			fail("No exception found");
		}catch(IntersectsAsSegmentException e){
			e.printStackTrace();
			fail("IntersectsAsSegmentException found");
		} catch (IntersectsAsPointException e) {/*ok*/}
		
		//Test case 4: S1 horizontal, S2 point, intersection at (0,0)
		s1 = new Segment(new Point(-5,0), new Point(5,0));
		s2 = new Segment(new Point(0,0), new Point(0,0));
		try{
			assertTrue(s1.pointIntersection(s2).getX()==0);
			assertTrue(s1.pointIntersection(s2).getY()==0);
			assertTrue(s2.pointIntersection(s1).getX()==0);
			assertTrue(s2.pointIntersection(s1).getY()==0);
		}catch(IntersectsAsSegmentException e){
			e.printStackTrace();
			fail("IntersectsAsSegmentException found");
		}
		
		//Test case 5: S1 vertical, S2 point, intersection at (0,0)
		s1 = new Segment(new Point(0,-5), new Point(0,5));
		s2 = new Segment(new Point(0,0), new Point(0,0));
		try{
			assertTrue(s1.pointIntersection(s2).getX()==0);
			assertTrue(s1.pointIntersection(s2).getY()==0);
			assertTrue(s2.pointIntersection(s1).getX()==0);
			assertTrue(s2.pointIntersection(s1).getY()==0);
		}catch(IntersectsAsSegmentException e){
			e.printStackTrace();
			fail("IntersectsAsSegmentException found");
		}
		
		//Test case 6: S1 and S2 points, intersection at (0,0)
		s1 = new Segment(new Point(0,0), new Point(0,0));
		s2 = new Segment(new Point(0,0), new Point(0,0));
		try{
			assertTrue(s1.pointIntersection(s2).getX()==0);
			assertTrue(s1.pointIntersection(s2).getY()==0);
			assertTrue(s2.pointIntersection(s1).getX()==0);
			assertTrue(s2.pointIntersection(s1).getY()==0);
		}catch(IntersectsAsSegmentException e){
			e.printStackTrace();
			fail("IntersectsAsSegmentException found");
		}
	}

	public void testSegmentIntersection() {
		//Test case 1: Both vertical, no intersection
		s1 = new Segment(new Point(1,0), new Point(1,2));
		s2 = new Segment(new Point(0,0), new Point(0,2));
		try{
			assertTrue(s1.segmentIntersection(s2)==null);
			assertTrue(s2.segmentIntersection(s1)==null);
			s1.pointIntersection(s2);
			s2.pointIntersection(s1);
		}catch(IntersectsAsPointException e){
			e.printStackTrace();
			fail("IntersectsAsPointException found");
		} catch (IntersectsAsSegmentException e) {/* ok */}
		
		
		//Test case 2: Both vertical, intersection = ((0,1)(0,2))
		s1 = new Segment(new Point(0,0), new Point(0,2));
		s2 = new Segment(new Point(0,1), new Point(0,3));
		try{
			assertTrue(s1.segmentIntersection(s2).getP1().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP1().getY()==1);
			assertTrue(s1.segmentIntersection(s2).getP2().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getY()==2);
			assertTrue(s2.segmentIntersection(s1).getP1().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getY()==1);
			assertTrue(s2.segmentIntersection(s1).getP2().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getY()==2);
			s1.pointIntersection(s2);
			s2.pointIntersection(s1);
		}catch(IntersectsAsPointException e){
			e.printStackTrace();
			fail("IntersectsAsPointException found");
		} catch (IntersectsAsSegmentException e) {/* ok */}
		
		//Test case 3: Both diagonal (upwards), no intersection
		s1 = new Segment(new Point(0,0), new Point(2,2));
		s2 = new Segment(new Point(0,1), new Point(2,3));
		try{
			assertTrue(s1.segmentIntersection(s2)==null);
			assertTrue(s2.segmentIntersection(s1)==null);
			s1.pointIntersection(s2);
			s2.pointIntersection(s1);
		}catch(IntersectsAsPointException e){
			e.printStackTrace();
			fail("IntersectsAsPointException found");
		} catch (IntersectsAsSegmentException e) {/* ok */}
		
		//Test case 4: Both diagonal (downwards), intersection = ((0,0)(1,-1))
		s1 = new Segment(new Point(-1,1), new Point(1,-1));
		s2 = new Segment(new Point(0,0), new Point(3,-3));
		try{
			assertTrue(s1.segmentIntersection(s2).getP1().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP1().getY()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getX()==1);
			assertTrue(s1.segmentIntersection(s2).getP2().getY()==-1);
			assertTrue(s2.segmentIntersection(s1).getP1().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getY()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getX()==1);
			assertTrue(s2.segmentIntersection(s1).getP2().getY()==-1);
			s1.pointIntersection(s2);
			s2.pointIntersection(s1);
		}catch(IntersectsAsPointException e){
			e.printStackTrace();
			fail("IntersectsAsPointException found");
		} catch (IntersectsAsSegmentException e) {/* ok */}


		//Test case 5: Vertical and Point, intersection at 0,0
		s1 = new Segment(new Point(0,-3), new Point(0,3));
		s2 = new Segment(new Point(0,0), new Point(0,0));
		try{
			assertTrue(s1.segmentIntersection(s2).getP1().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP1().getY()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getY()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getY()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getY()==0);
			s1.pointIntersection(s2);
			s2.pointIntersection(s1);
		}catch(IntersectsAsPointException e){
			e.printStackTrace();
			fail("IntersectsAsPointException found");
		} catch (IntersectsAsSegmentException e) {/* ok */}
		
		//Test case 6: horizontal and Point, intersection at 0,0
		s1 = new Segment(new Point(-3,0), new Point(3,0));
		s2 = new Segment(new Point(0,0), new Point(0,0));
		try{
			assertTrue(s1.segmentIntersection(s2).getP1().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP1().getY()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getY()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getY()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getY()==0);
			s1.pointIntersection(s2);
			s2.pointIntersection(s1);
		}catch(IntersectsAsPointException e){
			e.printStackTrace();
			fail("IntersectsAsPointException found");
		} catch (IntersectsAsSegmentException e) {/* ok */}
		
		//Test case 6: Point and Point, intersection at 0,0
		s1 = new Segment(new Point(0,0), new Point(0,0));
		s2 = new Segment(new Point(0,0), new Point(0,0));
		try{
			assertTrue(s1.segmentIntersection(s2).getP1().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP1().getY()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getX()==0);
			assertTrue(s1.segmentIntersection(s2).getP2().getY()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP1().getY()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getX()==0);
			assertTrue(s2.segmentIntersection(s1).getP2().getY()==0);
			s1.pointIntersection(s2);
			s2.pointIntersection(s1);
		}catch(IntersectsAsPointException e){
			e.printStackTrace();
			fail("IntersectsAsPointException found");
		} catch (IntersectsAsSegmentException e) {/* ok */}
	}
}
